//
//  VisitorLoginView.swift
//  SinaWeibo
//
//  Created by 周小伟 on 16/8/22.
//  Copyright © 2016年 周小伟. All rights reserved.
//

import UIKit

protocol VisitorLoginViewDelegate:NSObjectProtocol {
    //protocol function
    //login
    func visitorWillLogin()
    //register
    func visitorWillRegister()
}

class VisitorLoginView: UIView {
    
    //delegate property
    //property's value is strong reference!
    weak var visitorDelegate:VisitorLoginViewDelegate?
    
    func loginDidClicked(){
        self.visitorDelegate?.visitorWillLogin()
    }
    
    func registerDidClicked(){
        self.visitorDelegate?.visitorWillRegister()
    }
    
    //Mark: set basic information for page
    func setUIInfo(imageName:String?,title:String){
        iconView.hidden = false
        tipLabel.text = title
        if imageName != nil{
            circleView.image = UIImage(named: imageName!)
            self.bringSubviewToFront(circleView)
            iconView.hidden = true
        }else{
            self.startAnimation()
        }
        
    }
    
    private func startAnimation(){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.repeatCount = MAXFLOAT
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.removedOnCompletion = false
        circleView.layer.addAnimation(animation, forKey: nil)
    }
    
    //overide the init function
    init(){
        super.init(frame: CGRectZero)
        self.setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        self.addSubview(circleView)
        self.addSubview(backView)
        self.addSubview(iconView)
        self.addSubview(tipLabel)
        self.addSubview(loginBtn)
        self.addSubview(registerBtn)
        
        
        for v in self.subviews{
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        
        //vfl
        //"view1.attr1 = view2.attr2 * multiplier + constant"
        //Animation
        self.addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -60))
        
        //imageView
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        
        //layout the label
        self.addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 26))
        
        self.addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        self.addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50))
        
        //login
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //register btn
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .CenterY, relatedBy: .Equal, toItem: loginBtn, attribute: .CenterY, multiplier: 1.0, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: loginBtn, attribute: .Width, multiplier: 1.0, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: loginBtn, attribute: .Height, multiplier: 1.0, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        //metrics means the values that width and height of the view
        self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
        self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-45)-[registerBtn]", options: [], metrics: nil, views: ["backView":backView,"registerBtn":registerBtn]))
        
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        //set backView layout
//        self.addConstraint(NSLayoutConstraint(item: backView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
//        self.addConstraint(NSLayoutConstraint(item: backView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        loginBtn.addTarget(self, action: #selector(VisitorLoginView.loginDidClicked), forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: #selector(VisitorLoginView.registerDidClicked), forControlEvents: .TouchUpInside)
    }
    
    private lazy var circleView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    private lazy var iconView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_house"))
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_mask_smallicon"))
    
    //tips text
    private lazy var tipLabel:UILabel = UILabel(title: "关注一些人，回这里看看，有什么惊喜", size: 14, color: UIColor.lightGrayColor())
    private lazy var loginBtn:UIButton = UIButton(title: "登陆", backImage: "common_button_white_disable", color: UIColor.lightGrayColor())
    private lazy var registerBtn:UIButton = UIButton(title: "注册", backImage: "common_button_white_disable", color: UIColor.orangeColor())
}
